/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <rom.prevot@gmail.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return R. Prevot
 * ----------------------------------------------------------------------------
 */

/******************************************************************************\
 *          ____                _______    _____ _             _              *
 *         |  _ \              |__   __|  / ____| |           | |             *
 *         | |_) | ___  _ __ _ __ | | ___| (___ | |_ ___   ___| | __          *
 *         |  _ < / _ \| '__| '_ \| |/ _ \\___ \| __/ _ \ / __| |/ /          *
 *         | |_) | (_) | |  | | | | | (_) |___) | || (_) | (__|   <           *
 *         |____/ \___/|_|  |_| |_|_|\___/_____/ \__\___/ \___|_|\_\          *
 *                                                                            *
\******************************************************************************/

package app.core;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;

/**
 *
 * @author Ghost
 */
public class IncludePanel extends JPanel {

    JPanel controlPanel;
    Map<JButton, IncludedPan> panels;
    Component parent;
    ActionListener actionMngr;

    /**
     * Constructor
     */
    public IncludePanel(Component parent) {
        this.setLayout(new BorderLayout(0, 0));
        this.parent = parent;
        actionMngr = new ButtonActionListener(this);

        controlPanel = new JPanel();
        //controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
        controlPanel.setLayout(new GridLayout(10, 1));

        panels = new HashMap<JButton, IncludedPan>();

        this.add(BorderLayout.WEST, controlPanel);
    }

    public IncludePanel() {
    }

    public void addPanel(IncludedPan pan) {
        GButton bt = new GButton(pan.getName());

        panels.put(bt, pan);
        bt.addActionListener(actionMngr);
        controlPanel.add(bt);
    }

    public void showPan(int i) {
        removeAll();
        revalidate();
        repaint();

        add(BorderLayout.WEST, controlPanel);

        GButton btSource = (GButton) controlPanel.getComponent(i);
        if(btSource == null)
            return;
        
        for (Component c : controlPanel.getComponents()) {
            if (c instanceof GButton) {
                c.setEnabled(true);
            }
        }
        btSource.setEnabled(false);
        JComponent comp = panels.get(btSource).getComponent();
        add(BorderLayout.CENTER, comp);

        revalidate();
        repaint();
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public class ButtonActionListener implements ActionListener {

        IncludePanel parent;

        public ButtonActionListener(IncludePanel parent) {
            this.parent = parent;
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            parent.removeAll();
            parent.revalidate();
            parent.repaint();

            parent.add(BorderLayout.WEST, controlPanel);

            JButton btSource = (JButton) e.getSource();
            for (Component c : controlPanel.getComponents()) {
                if (c instanceof JButton) {
                    c.setEnabled(true);
                }
            }
            btSource.setEnabled(false);
            JComponent comp = parent.panels.get(btSource).getComponent();
            parent.add(BorderLayout.CENTER, comp);

            validate();
            parent.revalidate();
            parent.repaint();

        }
    }
}
